package org.hgvc.webapp.ets.spring.dao;

import java.util.List;

import org.hgvc.webapp.ets.bo.Agent;
import org.hgvc.webapp.ets.bo.AgreementControl;
import org.hgvc.webapp.ets.bo.CalendarCategoryDtl;
import org.hgvc.webapp.ets.bo.ComplimentaryBooking;
import org.hgvc.webapp.ets.bo.Member;
import org.hgvc.webapp.ets.bo.MemberBooking;
import org.hgvc.webapp.ets.bo.RCIBooking;
import org.hgvc.webapp.ets.bo.RCIBookingDetail;
import org.hgvc.webapp.ets.constant.BookingConstant;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.jocsb.base.AbstractHibernateDao;


public class BookingHbmDao extends AbstractHibernateDao{
	public List<ComplimentaryBooking> getComplimentaryBookingList()throws Exception{
		Criteria criteria=getSession().createCriteria(ComplimentaryBooking.class);
		criteria.add(Restrictions.eq("isDeleted", CommonConstant.NOT_DELETED));
		List<ComplimentaryBooking> list=criteria.list();
		return list;
	}
	 
	public List<CalendarCategoryDtl> getCalendarCategoryDtls(String calendarCatId)throws Exception{
		StringBuffer strb = new StringBuffer();
		strb.append(" select c from CalendarCategoryDtl c where ");
		strb.append("   c.calCat.calendarCatId=:calendarCatId and c.isDeleted=:isDeleted");
		
		Query query = getSession().createQuery(strb.toString());
		query.setParameter("calendarCatId", calendarCatId);
		query.setParameter("isDeleted", CommonConstant.NOT_DELETED);
		
		return (List<CalendarCategoryDtl>)query.list();
	}
	
	public List<ComplimentaryBooking> getComplimentaryBookingList(String searchField,String searchKeyword)throws Exception{
		Criteria criteria=getSession().createCriteria(ComplimentaryBooking.class);
		criteria.add(Restrictions.eq("isDeleted", CommonConstant.NOT_DELETED));
		
		if (searchField.equalsIgnoreCase(BookingConstant.SEARCH_BOOKING_NO))
			criteria.add(Restrictions.ilike("bookingNo", "%"+searchKeyword+"%"));
		
		if (searchField.equalsIgnoreCase(BookingConstant.SEARCH_CONFIRMATION_NO))
			criteria.add(Restrictions.ilike("confirmationNo", "%"+searchKeyword+"%"));
		
		List<ComplimentaryBooking> list=criteria.list();
		return list;
	}
	
	public List<RCIBooking> getRCIBookingList()throws Exception{
		Criteria criteria = getSession().createCriteria(RCIBooking.class);
		criteria.add(Restrictions.eq("isDeleted", CommonConstant.NOT_DELETED));
		List<RCIBooking> list=criteria.list();
		return list;
	}
	
	public List<RCIBookingDetail> getRCIBookingDetailList(RCIBooking rciBooking)throws Exception{
		Criteria criteria = getSession().createCriteria(RCIBookingDetail.class);
		criteria.add(Restrictions.eq("isDeleted", CommonConstant.NOT_DELETED));
		criteria.add(Restrictions.eq("rciBooking.rciBookingId", rciBooking.getRciBookingId()));
		List<RCIBookingDetail> list=criteria.list();
		return list;
	}
	
	public List<MemberBooking> getMemberBookingList(String bookingStatus)throws Exception{
		Criteria criteria = getSession().createCriteria(MemberBooking.class);
		criteria.add(Restrictions.eq("isDeleted", CommonConstant.NOT_DELETED));
		criteria.add(Restrictions.eq("status", bookingStatus));
		List<MemberBooking> list = criteria.list();
		return list;
	}
	
	public List<MemberBooking> getMemberBookingList(Agent agent, String bookingStatus)throws Exception{
		Criteria criteria = getSession().createCriteria(MemberBooking.class);
		criteria.add(Restrictions.eq("isDeleted", CommonConstant.NOT_DELETED));
		criteria.add(Restrictions.eq("status", bookingStatus));
		Criteria memCrt = criteria.createCriteria("member");
		Criteria agentCrt = memCrt.createCriteria("agent");
		agentCrt.add(Restrictions.eq("agentId", agent.getAgentId()));
		List<MemberBooking> list = criteria.list();
		return list;
	}
	
	public List<MemberBooking> getMemberBookingList(Member member, String bookingStatus)throws Exception{
		Criteria criteria = getSession().createCriteria(MemberBooking.class);
		criteria.add(Restrictions.eq("isDeleted", CommonConstant.NOT_DELETED));
		criteria.add(Restrictions.eq("status", bookingStatus));
		Criteria memCrt = criteria.createCriteria("member");
		memCrt.add(Restrictions.eq("memberId", member.getMemberId()));
		List<MemberBooking> list = criteria.list();
		return list;
	}
	
	public List<MemberBooking> getMemberBookingList(String searchField,String searchKeyword,String bookingStatus)throws Exception{
		Criteria criteria = getSession().createCriteria(MemberBooking.class);
		criteria.add(Restrictions.eq("isDeleted", CommonConstant.NOT_DELETED));
		criteria.add(Restrictions.eq("status", bookingStatus));
		if (searchField.equalsIgnoreCase(BookingConstant.SEARCH_BOOKING_NO))
			criteria.add(Restrictions.ilike("memberBookingNo", "%"+searchKeyword+"%"));
		
		if (searchField.equalsIgnoreCase(BookingConstant.SEARCH_CONFIRMATION_NO))
			criteria.add(Restrictions.ilike("confirmationNo", "%"+searchKeyword+"%"));

		List<MemberBooking> list = criteria.list();
		return list;
	}
	
	public List<MemberBooking> getMemberBookingList(Agent agent,String searchField,String searchKeyword,String bookingStatus)throws Exception{
		Criteria criteria = getSession().createCriteria(MemberBooking.class);
		criteria.add(Restrictions.eq("isDeleted", CommonConstant.NOT_DELETED));
		criteria.add(Restrictions.eq("status", bookingStatus));
		if (searchField.equalsIgnoreCase(BookingConstant.SEARCH_BOOKING_NO))
			criteria.add(Restrictions.ilike("memberBookingNo", "%"+searchKeyword+"%"));
		
		if (searchField.equalsIgnoreCase(BookingConstant.SEARCH_CONFIRMATION_NO))
			criteria.add(Restrictions.ilike("confirmationNo", "%"+searchKeyword+"%"));

		Criteria memCrt = criteria.createCriteria("member");
		Criteria agentCrt = memCrt.createCriteria("agent");
		agentCrt.add(Restrictions.eq("agentId", agent.getAgentId()));
		
		List<MemberBooking> list = criteria.list();
		return list;
	}
	
	public List<MemberBooking> getMemberBookingList(Member member,String searchField,String searchKeyword,String bookingStatus)throws Exception{
		Criteria criteria = getSession().createCriteria(MemberBooking.class);
		criteria.add(Restrictions.eq("isDeleted", CommonConstant.NOT_DELETED));
		criteria.add(Restrictions.eq("status", bookingStatus));
		if (searchField.equalsIgnoreCase(BookingConstant.SEARCH_BOOKING_NO))
			criteria.add(Restrictions.ilike("memberBookingNo", "%"+searchKeyword+"%"));
		
		if (searchField.equalsIgnoreCase(BookingConstant.SEARCH_CONFIRMATION_NO))
			criteria.add(Restrictions.ilike("confirmationNo", "%"+searchKeyword+"%"));

		Criteria memCrt = criteria.createCriteria("member");
		memCrt.add(Restrictions.eq("memberId", member.getMemberId()));
		
		List<MemberBooking> list = criteria.list();
		return list;
	}
	
	
}
